﻿using AspNetTraining.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspNetTraining
{
    public partial class Permissions : System.Web.UI.Page
    {
        PermissionManager PerManager = new PermissionManager();
        public UserAccount SessionLogin
        {
            get
            {
                return (UserAccount)Session["LoggedUser"];
            }
            set
            {
                Session["LoggedUser"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (SessionLogin == null)
                {
                    Response.Redirect("~/Account/Login.aspx?returnUrl=" + Request.Url.PathAndQuery);
                }
                else
                {
                    if (Request["id"] != null)
                    {
                        PerManager.DeletePermission(new Guid(Request["id"].ToString()));
                        Response.Redirect("~/Permissions.aspx");
                        return;
                    }
                    gvPermission.DataSource = PerManager.GetAllPermission();
                    gvPermission.DataBind();
                }
            }
            
        }
        protected String GetUserNameByID(object ID)
        {
            if (ID == null)
                return string.Empty;
            GroupUserManager groupuser = new GroupUserManager();
            return groupuser.GetGroupUserNameByID(new Guid(ID.ToString()));
        }
        /// <summary>
        ///  return GroupUser's Name
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        protected String GetGroupUserByID(object ID)
        {
            if (ID == null)
                return string.Empty;
            GroupUserManager groupuser = new GroupUserManager();
            return groupuser.GetGroupUserNameByID(new Guid(ID.ToString()));
        }
        /// <summary>
        /// return Function's Name
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        protected String GetNameFunctionByID(object ID)
        {
            if (ID == null)
                return string.Empty;
            FunctionalityManager funcmanager = new FunctionalityManager();
            return funcmanager.GetNameFunctionByID(new Guid(ID.ToString()));
        }
        protected void Deletes_Click(object sender,EventArgs e) {

            foreach (GridViewRow row in gvPermission.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkDelete");
                if (chk.Checked)
                {
                    PermissionManager perm = new PermissionManager();
                    Label lbl = (Label)row.FindControl("lblID");
                    perm.DeletePermission(new Guid(lbl.Text));
                }
            }
            Response.Redirect("~/Permissions.aspx");
        }
    }
}